<?php
/**
 * new develop: 消息行为，用于绑定对应的模型model上
 */
namespace common\components\services\msg;

use common\components\bases\BaseBehavior;
use common\components\helpers\Fun;
use common\components\helpers\MsgeventData;

class MsgBehavior extends BaseBehavior {
    /*
     * 通用的插入更新自动调用这个方法，但是根据我们的变态需求需要单独开发的方法几率更大
     */
    public function afterInsert($event){
        $eventKey = (new MsgeventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $extra_declare = (new MsgeventData())->getExtraDeclare($eventKey);
        if(!empty($extra_declare) && isset($extra_declare['extrafields'])){
            $extra_fields = $extra_declare['extrafields'];
            $this->insertSend($event,$eventKey,$extra_fields);
        }else{
            $this->insertSend($event,$eventKey);
        }
        /**$messageManage=\Yii::createObject([
            'class'=>MsgEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$this->owner->toArray(),
            'eventKey'=>$eventKey
        ]);
        $messageManage->afterInsertSend();*/
    }

    //发送短信验证码
    public function sendTelephoneValidateCode($event){
        \Yii::error('send telephone start');
        $eventKey=(new MsgeventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();

        $messageManage=\Yii::createObject([
            'class'=>MsgEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$event->action['data'],
            'eventKey'=>$eventKey
        ]);
        \Yii::error('---------------data--------');
        \Yii::error($event->action['data']);

        $messageManage->afterInsertSend();
    }

    public function remindInfo($event){
        $eventKey = (new MsgeventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        if(!empty($eventKey)){
            $extra_declare = (new MsgeventData())->getExtraDeclare($eventKey);
        }else{
            return;
        }
        if(!empty($extra_declare)) {
            $extra_fields = isset($extra_declare['extrafields']) ? $extra_declare['extrafields'] : [];
            $ownerarray = $this->owner->toArray([], $extra_fields);
        }else{
            $ownerarray = $this->owner->toArray();
        }
        $messageManage=\Yii::createObject([
            'class'=>MsgEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$ownerarray,
            'eventKey'=>$eventKey
        ]);
        $messageManage->afterInsertSend();
    }

    /*
     * 下单之后执行的发消息操作,针对订单的独特操作不适用于通用的插入更新
     */
    public function afterUpdate($event){
        //有两个相同的该怎么办
        $eventKeys=(new MsgeventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKeys();
        if(!empty($eventKeys) && count($eventKeys)){
            $model = $this->owner;//得到当前触发的模型
            $changeattr = $event->changedAttributes;//获取update执行时更改的
            $attr = $this->owner->getAttributes();
            foreach ($eventKeys as $eventKey) {
                if(!empty($eventKey)){
                    $extra_declare = (new MsgeventData())->getExtraDeclare($eventKey);
                    if(!empty($extra_declare)){
                        $extra_fields = isset($extra_declare['extrafields'])?$extra_declare['extrafields']:null;
                        if(isset($extra_declare['changefields'])){
                            foreach ($extra_declare['changefields'] as $k=>$ed){//目前代码是变更字段满足一个就会进行触发，后期如果需要多个条件共存的话在进行修改
                                if(isset($changeattr["$k"]) && $changeattr["$k"] != $attr["$k"] && in_array($model->$k,$ed)){
                                    $this->insertSend($event,$eventKey,$extra_fields);
                                    break;
                                }else{
                                    continue;
                                }
                            }
                        }else{//不存在按照指定字段进行操作的话那就是所有的更新都会进行消息的产生和发送
                            $this->insertSend($event,$eventKey,$extra_fields);
                        }
                    }else{
                        $this->insertSend($event,$eventKey);
                    }
                }else{
                    continue;
                }
            }
        }
//        $extra_declare = null;
//        if(!empty($eventKey)){
//            $extra_declare = (new MsgeventData())->getExtraDeclare($eventKey);
//        }else{
//            return;
//        }
//        $model = $this->owner;//得到当前触发的模型
//        $changeattr = $event->changedAttributes;//获取update执行时更改的
//        $attr = $this->owner->getAttributes();
//        if(!empty($extra_declare)){
//            $extra_fields = isset($extra_declare['extrafields'])?$extra_declare['extrafields']:[];
//            if(isset($extra_declare['changefields'])){
//                foreach ($extra_declare['changefields'] as $k=>$ed){//目前代码是变更字段满足一个就会进行触发，后期如果需要多个条件共存的话在进行修改
//                    if(isset($changeattr["$k"]) && $changeattr["$k"] != $attr["$k"] && in_array($model->$k,$ed)){
//                        $this->insertSend($event,$eventKey,$extra_fields);
//                        break;
//                    }
//                }
//            }else{
//                $this->insertSend($event,$eventKey,$extra_fields);
//            }
//        }else{//没有任何附加条件，只要更新就进行插入操作
//            $this->insertSend($event,$eventKey);
//        }
    }

    public function insertSend($event=null,$eventKey=null,$extra_fields=null){
        if(!empty($event) && !empty($eventKey)){
            if($extra_fields == null){
                $ownerArray = $this->owner->toArray();
            }else{
                $ownerArray = $this->owner->toArray([],$extra_fields);
            }
            $messageManage=\Yii::createObject([
                'class'=>MsgEventManage::className(),
                'eventArray'=>$event,
                'ownerArray'=>$ownerArray,
                'eventKey'=>$eventKey
            ]);
            $messageManage->afterInsertSend();
        }else{
            return;
        }

    }
    /*
     * 通用消息发送
     */
    public function handerSend($event){
        $eventKey = (new MsgeventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $messageManage=\Yii::createObject([
            'class'=>MsgEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$this->owner->toArray(),
            'eventKey'=>$eventKey
        ]);
        $messageManage->afterInsertSend();
    }
}